import { defineStore } from "pinia";
import { cloneDeep } from "lodash-es";
import { STORE_PREFIX } from "@/utils/constant";
import { reqBasisConfig } from "@/api/setting";
import { reqRoomTotal } from "@/api/main";

export const useRoomStore = defineStore(
  `${STORE_PREFIX}room_store`,
  () => {
    const roomInfo = ref<BasisConfigMo | null>(null); // 房间信息
    const getRoomInfo = computed(() => roomInfo.value);
    const roomTotal = ref<RoomTotalMo | null>(null);
    const applyVoice = ref(1);
    const applyCount = ref(0);
    const joinCount = ref(0);

    // 设置房间信息
    const setRoomInfo = (info: BasisConfigMo | null) => {
      roomInfo.value = cloneDeep(info);
    };

    // 请求房间信息
    const actionRoomInfo = async () => {
      try {
        const res = await reqBasisConfig();
        roomInfo.value = res.data;
      } catch (error) {
        console.log(error);
      }
    };

    // 请求房间信息
    const actionRoomTotal = async () => {
      try {
        const res = await reqRoomTotal();
        roomTotal.value = res.data;
      } catch (error) {
        console.log(error);
      }
    };

    return {
      roomInfo,
      roomTotal,
      applyVoice,
      applyCount,
      joinCount,
      getRoomInfo,
      setRoomInfo,
      actionRoomInfo,
      actionRoomTotal
    };
  },
  {
    persist: {
      storage: sessionStorage
    }
  }
);
